//
//  PushManager.m
//  YJAPPThird
//
//  Created by 张吉旭 on 16/12/21.
//  Copyright © 2016年 yj. All rights reserved.
//

#import "PushManager.h"
#import <AdSupport/AdSupport.h>
#import "ThirdDefine.h"
#import "PushViewManager.h"

static PushManager *instance = nil;

@implementation PushManager

+(PushManager*)shareInstance{
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        instance = [[PushManager alloc] init];
    });
    
    return instance;
}


-(void)handleRemoteNotification:(NSDictionary *)remoteInfo{
    [JPUSHService handleRemoteNotification:remoteInfo];
}

-(void)registerDeviceToken:(NSData *)deviceToken{
    [JPUSHService registerDeviceToken:deviceToken];
}


-(void)setUpJPush:(NSDictionary*)launchOptions{
    
    self.JPushId = DEF_PERSISTENT_GET_OBJECT(DEF_PERSISTENT_JPUSHID);
    if(!self.JPushId){
        self.JPushId = @"";
    }
    
    //初始APNs
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    
    // Optional
    // 获取IDFA
    // 如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值
    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    
    // Required
    
    
    BOOL isProductionBool = isProductionB;
    
    [JPUSHService setupWithOption:launchOptions appKey:JPshAppKey
                          channel:JPshChannel
                 apsForProduction:isProductionBool
            advertisingIdentifier:advertisingId];
    
    
    WeakSelf;
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if(resCode == 0){
            NSLog(@"registrationID获取成功：%@",registrationID);
            weakSelf.JPushId = registrationID;
            DEF_PERSISTENT_SET_OBJECT(registrationID, DEF_PERSISTENT_JPUSHID);
        }
        else{
            NSLog(@"registrationID获取失败，code：%d",resCode);
            weakSelf.JPushId = @"";
        }
    }];
}


/*
 * @brief handle UserNotifications.framework [willPresentNotification:withCompletionHandler:]
 * @param center [UNUserNotificationCenter currentNotificationCenter] 新特性用户通知中心
 * @param notification 前台得到的的通知对象
 * @param completionHandler 该callback中的options 请使用UNNotificationPresentationOptions
 */
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler{
    
    // Required
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
        
        
//        [self locationActionToView:userInfo];
    }else{
        //本地通知
    }
//    completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法，选择是否提醒用户，有Badge、Sound、Alert三种类型可以选择设置
    
}
/*
 * @brief handle UserNotifications.framework [didReceiveNotificationResponse:withCompletionHandler:]
 * @param center [UNUserNotificationCenter currentNotificationCenter] 新特性用户通知中心
 * @param response 通知响应对象
 * @param completionHandler
 */
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
    
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
        
        
        [self locationActionToView:userInfo];
    }else{
        //本地通知
    }
    completionHandler();  // 系统要求执行这个方法
}


//定位，指定到对应的页面
-(void)locationActionToView:(NSDictionary *)userInfo{
    
    [[PushViewManager shareInstance] pushViewByUserInfo:userInfo];
}

@end
